package com.dragon.mmochat.model.scheduleJob;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfAddBlood;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfNormal;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfRescueTeamer;
import com.dragon.mmochat.model.enumType.MmochatBossLevelType;
import com.dragon.mmochat.model.enumType.MmochatFamilyType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatPoint;
import com.dragon.mmochat.model.role.MmochatMonsterFactory;
import com.dragon.mmochat.model.role.MmochatTempNpc;
import com.dragon.mmochat.model.task.MmochatNpcBattle;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.dragon.mmochat.util.MmochatUtil;

//蒲牢十子
public class Hourly_TimeMonster implements Job {
	private static Logger log = LoggerFactory
			.getLogger(Hourly_TimeMonster.class);

	@SuppressWarnings("deprecation")
	@Override
	public void execute(JobExecutionContext arg0) {
		try {
			log.debug("发布蒲牢十子");
			Date now = new Date();
			String[] 天干 = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚",
					"辛", "壬", "癸" };
			String[] 地支 = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午",
					"未", "申", "酉", "戌", "亥" };
			int index = ((now.getHours() + 1) / 2) % 12;
			String nameEnd = 地支[index];
			Map<Integer, Integer[]> bossInfo = new HashMap<Integer, Integer[]>();
			bossInfo.put(35, new Integer[] { 2, 10, 11, 1, 23 });
			bossInfo.put(45, new Integer[] { 16, 3, 19, 20 });
			bossInfo.put(55, new Integer[] { 19, 20, 12 });
			bossInfo.put(65, new Integer[] { 12, 13, 14 });
			bossInfo.put(75, new Integer[] { 14, 9, 13, 8 });
			bossInfo.put(85, new Integer[] { 18, 9, 8 });
			bossInfo.put(95, new Integer[] { 6, 7, 15 });
			bossInfo.put(105, new Integer[] { 15, 21, 17 });
			bossInfo.put(115, new Integer[] { 17, 22 });
			bossInfo.put(125, new Integer[] { 54 });
			bossInfo.put(135, new Integer[] { 55 });
			bossInfo.put(145, new Integer[] { 56 });
			bossInfo.put(155, new Integer[] { 57 });
			bossInfo.put(165, new Integer[] { 58 });
			bossInfo.put(175, new Integer[] { 59 });
			bossInfo.put(185, new Integer[] { 60 });
			bossInfo.put(195, new Integer[] { 61 });
			bossInfo.put(205, new Integer[] { 62 });
			bossInfo.put(215, new Integer[] { 63 });

			for (int line = 0; line < MmochatConstant.maxMapLineNum; line++) {
				for (Entry<Integer, Integer[]> entry : bossInfo.entrySet()) {
					if (MmochatUtil.isInPercent(50)){
						continue;
					}
					Integer[] mapIds = entry.getValue();
					if (mapIds == null || mapIds.length == 0) {
						continue;
					}
					int mapIndex = MmochatUtil.getRandomValue(mapIds.length);
					int mapId = mapIds[mapIndex];
					int level = entry.getKey();
					MmochatPoint point = MmochatMapService
							.getRandReachablePoint(mapId);
					if (point != null) {
						MmochatTempNpc boss = new MmochatTempNpc(true);
						MmochatNpcService.npcs.put(boss.getNpcId(), boss);
						boss.getFunctionItems().add(MmochatNpcItemType.击杀);
						boss.setLevel(level);
						boss.setLifeTime(3600000);
						boss.setLine(line);
						int lifeTurn = (level - 120) / 10;
						if (lifeTurn > 0) {
							boss.setName(lifeTurn + "转-蒲牢十子");
						} else {
							boss.setName(level + "级-蒲牢十子");
						}
						boss.setBmpType(MmochatRoleBmpType.蛇妖);
						boss.setMapId(mapId);
						short x = (short) (point.getX() * 24 + Math.random() * 24);
						short y = (short) (point.getY() * 24 + Math.random() * 24);
						boss.setX(x);
						boss.setY(y);
						MmochatMap theMap = MmochatMapService.maps.get(mapId);
						if (theMap == null) {
							continue;
						}
						log.debug(level + "级-蒲牢十子出现在" + theMap.getMapName()
								+ (line + 1) + "线(" + x + "," + y + ")");
						theMap.addNpc(boss.getLine(), boss);

						MmochatTask newTask = new MmochatTask();
						newTask.setTaskId(0);
						boss.addTask(newTask);
						MmochatNpcBattle battle = new MmochatNpcBattle();
						newTask.setFinish_need_battles(battle);
						newTask.setTaskType(MmochatTaskType.击杀天干地支);
						MmochatMonsterFactory bigMonster = new MmochatMonsterFactory();
						bigMonster.setName("甲" + nameEnd + "兽");
						bigMonster.setLevel(level);
						bigMonster.setBmpType(MmochatRoleBmpType.蛇妖);
						bigMonster.setDao((int) (1.5 * MmochatBattleService
								.getStandardDao(level, 0)));
						bigMonster.setCanRescure(true);
						bigMonster.setAntiAll(10);
						bigMonster.setLevelType(MmochatBossLevelType.自身等级);
						bigMonster.setSuperPhysicalAttack(10);
						bigMonster.setSuperSpritAttack(10);

						MmochatAttackModeOfNormal attackMode = new MmochatAttackModeOfNormal();
						if (level < 30) {
							bigMonster.setEquipLevel(1);
							bigMonster.setTeamNum(3);
							bigMonster.setKillRound(6);
							bigMonster.setPhysical(0.5);
							bigMonster.setMagic(0.4);
							bigMonster.setBossMaxSkillTargetNum(5);
							bigMonster.setSpeedPointAddType(4);

							attackMode.setPhysicalAttackOpportunity((int) (64));
							attackMode.setMagicAttackOppotunity((int) (16));
							attackMode.setLimitOppotunity((int) (10));
							attackMode.setFuZhuOppotunity((int) 10);
						} else if (level < 60) {
							bigMonster.setEquipLevel(2);
							bigMonster.setTeamNum(3);
							bigMonster.setKillRound(6);
							bigMonster.setPhysical(0.5);
							bigMonster.setMagic(0.4);
							bigMonster.setBossMaxSkillTargetNum(5);
							bigMonster.setSpeedPointAddType(4);

							attackMode.setPhysicalAttackOpportunity((int) (64));
							attackMode.setMagicAttackOppotunity((int) (16));
							attackMode.setLimitOppotunity((int) (10));
							attackMode.setFuZhuOppotunity((int) 10);
						} else if (level < 90) {
							bigMonster.setEquipLevel(3);
							bigMonster.setTeamNum(4);
							bigMonster.setKillRound(6);
							bigMonster.setPhysical(0.6);
							bigMonster.setMagic(0.48);
							bigMonster.setBossMaxSkillTargetNum(5);
							bigMonster.setSpeedPointAddType(4);

							attackMode.setPhysicalAttackOpportunity((int) (56));
							attackMode.setMagicAttackOppotunity((int) (24));
							attackMode.setLimitOppotunity((int) (10));
							attackMode.setFuZhuOppotunity((int) 10);
						} else {
							bigMonster.setEquipLevel(4);
							bigMonster.setTeamNum(4);
							bigMonster.setKillRound(6);
							bigMonster.setPhysical(0.6);
							bigMonster.setMagic(0.48);
							bigMonster.setBossMaxSkillTargetNum(5);
							bigMonster.setSpeedPointAddType(4);

							attackMode.setPhysicalAttackOpportunity((int) (56));
							attackMode.setMagicAttackOppotunity((int) (24));
							attackMode.setLimitOppotunity((int) (10));
							attackMode.setFuZhuOppotunity((int) 10);
						}

						bigMonster.setAttackMode_normal(attackMode);

						MmochatAttackModeOfRescueTeamer rescueMode = new MmochatAttackModeOfRescueTeamer();
						rescueMode.setRescueOpportunity(20);
						rescueMode.setBloodPercent(10);
						bigMonster.setAttackMode_rescueTeamer(rescueMode);

						List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();
						for (int i = 0; i < 9; i++) {
							MmochatMonsterFactory monster = new MmochatMonsterFactory();
							monster.setName(天干[i + 1] + nameEnd + "兽");
							monster.setLevel(level);
							monster.setBmpType(MmochatRoleBmpType.蛇妖);
							monster.setDao((int) (MmochatBattleService
									.getStandardDao(level, 0)));
							monster.setCanRescure(true);
							monster.setAntiAll(10);
							monster.setLevelType(MmochatBossLevelType.自身等级);
							monster.setSuperPhysicalAttack(10);
							monster.setSuperSpritAttack(10);

							if (level < 30) {
								monster.setLevel(level);
								monster.setEquipLevel(1);
								monster.setTeamNum(3);
								monster.setKillRound(0.67);
								monster.setPhysical(0.12);
								monster.setMagic(0.084);
								monster.setBossMaxSkillTargetNum(2);
								monster.setSpeedPointAddType(1);

							} else if (level < 60) {
								monster.setLevel(level);
								monster.setEquipLevel(2);
								monster.setTeamNum(3);
								monster.setKillRound(0.67);
								monster.setPhysical(0.12);
								monster.setMagic(0.084);
								monster.setBossMaxSkillTargetNum(2);
								monster.setSpeedPointAddType(1);

							} else if (level < 90) {
								monster.setLevel(level);
								monster.setEquipLevel(3);
								monster.setTeamNum(4);
								monster.setKillRound(0.67);
								monster.setPhysical(0.18);
								monster.setMagic(0.126);
								monster.setBossMaxSkillTargetNum(2);
								monster.setSpeedPointAddType(1);

							} else {
								monster.setLevel(level);
								monster.setEquipLevel(4);
								monster.setTeamNum(4);
								monster.setKillRound(0.67);
								monster.setPhysical(0.18);
								monster.setMagic(0.126);
								monster.setBossMaxSkillTargetNum(2);
								monster.setSpeedPointAddType(1);

							}

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							attackMode1
									.setPhysicalAttackOpportunity((int) (74));
							attackMode1.setMagicAttackOppotunity((int) (16));
							attackMode1.setLimitOppotunity((int) 5);
							attackMode1.setFuZhuOppotunity((int) 5);
							monster.setAttackMode_normal(attackMode1);

							MmochatAttackModeOfRescueTeamer rescueMode1 = new MmochatAttackModeOfRescueTeamer();
							rescueMode1.setRescueOpportunity(20);
							rescueMode1.setBloodPercent(10);
							monster.setAttackMode_rescueTeamer(rescueMode1);
							smallMonster.add(monster);
						}
						battle.setBoss(bigMonster);
						battle.setSmallBoss(smallMonster);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 超级大BOSS
		try {
			int num = 0;
			Date now = new Date();
			if (now.getHours() == 12) {
				num = 2;
			} else if (now.getHours() == 21) {
				num = MmochatConstant.superBossNumInNight;
			}
			if (num == 0) {
				return;
			}
			int lifeMin = MmochatConstant.superBossLifeTime;// 存在的分钟数

			MmochatChatService.sendSystemMsg(MmochatUtil.wrapColor("各等级"
					+ MmochatUtil.wrapColor("战神", Color.red)
					+ "降临了，欢迎各级高手前往挑战!各等级段降临"
					+ MmochatUtil.wrapColor(num + "位战神", Color.green) + ","
					+ lifeMin + "分钟内如无人挑战的战神将会离开!", Color.yellow));

			log.debug("发布超级大BOSS");
			// level-->mapId
			Map<Integer, Integer> bossInfo = new HashMap<Integer, Integer>();
			bossInfo.put(35, 2);
			bossInfo.put(45, 16);
			bossInfo.put(55, 19);
			bossInfo.put(65, 12);
			bossInfo.put(75, 14);
			bossInfo.put(85, 18);
			bossInfo.put(95, 6);
			bossInfo.put(105, 15);
			bossInfo.put(115, 17);
			bossInfo.put(125, 54);
			bossInfo.put(135, 55);
			bossInfo.put(145, 56);
			bossInfo.put(155, 57);
			bossInfo.put(165, 58);
			bossInfo.put(175, 59);
			bossInfo.put(185, 60);
			bossInfo.put(195, 61);
			bossInfo.put(205, 62);
			bossInfo.put(215, 63);

			for (int s = 0; s < num; s++) {
				for (Entry<Integer, Integer> entry : bossInfo.entrySet()) {
					int mapId = entry.getValue();
					int line;
					if (num == MmochatConstant.maxMapLineNum) {
						line = s;
					} else {
						line = MmochatUtil
								.getRandomValue(MmochatConstant.maxMapLineNum);
					}
					int level = entry.getKey();
					MmochatPoint point = MmochatMapService
							.getRandReachablePoint(mapId);
					if (point != null) {
						MmochatTempNpc boss = new MmochatTempNpc(true);
						MmochatNpcService.npcs.put(boss.getNpcId(), boss);
						boss.getFunctionItems().add(MmochatNpcItemType.击杀);
						boss.setLevel(level);
						boss.setLifeTime(MmochatConstant.MS_PER_MINUTE
								* lifeMin);
						boss.setLine(line);
						int lifeTurn = (level - 120) / 10;
						if (lifeTurn > 0) {
							boss.setName(lifeTurn + "转-战神");
						} else {
							boss.setName(level + "级-战神");
						}
						boss.setBmpType(MmochatRoleBmpType.祖师爷);
						boss.setMapId(mapId);
						short x = (short) (point.getX() * 24 + Math.random() * 24);
						short y = (short) (point.getY() * 24 + Math.random() * 24);
						boss.setX(x);
						boss.setY(y);
						MmochatMap theMap = MmochatMapService.maps.get(mapId);
						if (theMap == null) {
							continue;
						}
						log.debug(boss.getName() + "出现在" + theMap.getMapName()
								+ (line + 1) + "线(" + x + "," + y + ")");
						theMap.addNpc(boss.getLine(), boss);

						MmochatTask newTask = new MmochatTask();
						newTask.setTaskId(0);
						boss.addTask(newTask);
						MmochatNpcBattle battle = new MmochatNpcBattle();
						newTask.setFinish_need_battles(battle);
						newTask.setTaskType(MmochatTaskType.击杀战神);
						MmochatMonsterFactory bigMonster = new MmochatMonsterFactory();
						bigMonster.setName("战神");
						bigMonster.setLevel(level);
						bigMonster.setBmpType(MmochatRoleBmpType.祖师爷);
						bigMonster.setDao((int) (12 * MmochatBattleService
								.getStandardDao(level, 0)));
						bigMonster.setCanRescure(false);
						bigMonster.setAntiAll(200);
						bigMonster.setLevelType(MmochatBossLevelType.自身等级);
						bigMonster.setSuperPhysicalAttack(5);
						bigMonster.setSuperSpritAttack(5);

						MmochatAttackModeOfNormal attackMode = new MmochatAttackModeOfNormal();
						bigMonster.setEquipLevel(20);
						bigMonster.setTeamNum(8);
						bigMonster.setKillRound(25);
						bigMonster.setPhysical(2.0);
						bigMonster.setMagic(1.2);
						bigMonster.setBossMaxSkillTargetNum(10);
						bigMonster.setSpeedPointAddType(3);

						attackMode.setPhysicalAttackOpportunity((int) (80));
						attackMode.setMagicAttackOppotunity((int) (20));

						bigMonster.setAttackMode_normal(attackMode);

						MmochatAttackModeOfAddBlood addBloodMode = new MmochatAttackModeOfAddBlood();
						addBloodMode.setTriggerBloodPercent(30);
						addBloodMode.setAddBloodOpportunity(20);
						addBloodMode.setAddBloodPercent(20);
						bigMonster.setAttackMode_addBlood(addBloodMode);

						List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();
						for (int i = 0; i < 2; i++) {
							MmochatMonsterFactory monster = new MmochatMonsterFactory();
							monster.setName("狂战士");
							monster.setLevel(level);
							monster.setFamily(MmochatFamilyType.截教);
							monster.setBmpType(MmochatRoleBmpType.截教男图);
							monster.setDao((int) (9 * MmochatBattleService
									.getStandardDao(level, 0)));
							monster.setCanRescure(false);
							monster.setAntiConfuse(100);
							monster.setLevelType(MmochatBossLevelType.自身等级);
							monster.setSuperPhysicalAttack(5);
							monster.setSuperSpritAttack(5);

							monster.setEquipLevel(20);
							monster.setTeamNum(8);
							monster.setKillRound(5);
							monster.setPhysical(1.2);
							monster.setMagic(0.8);
							monster.setBossMaxSkillTargetNum(3);
							monster.setSpeedPointAddType(0);

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							attackMode1
									.setPhysicalAttackOpportunity((int) (90));
							attackMode1.setMagicAttackOppotunity((int) (10));
							monster.setAttackMode_normal(attackMode1);
							smallMonster.add(monster);
						}

						for (int i = 0; i < 2; i++) {
							MmochatMonsterFactory monster = new MmochatMonsterFactory();
							monster.setName("意念师");
							monster.setLevel(level);
							monster.setFamily(MmochatFamilyType.阐教);
							monster.setBmpType(MmochatRoleBmpType.阐教女图);
							monster.setDao((int) (7 * MmochatBattleService
									.getStandardDao(level, 0)));
							monster.setCanRescure(false);
							monster.setAntiAll(80);
							monster.setLevelType(MmochatBossLevelType.自身等级);
							monster.setSuperPhysicalAttack(5);
							monster.setSuperSpritAttack(5);

							monster.setEquipLevel(20);
							monster.setTeamNum(8);
							monster.setKillRound(5);
							monster.setPhysical(0.3);
							monster.setMagic(0.2);
							monster.setBossMaxSkillTargetNum(3);
							monster.setSpeedPointAddType(2);

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							attackMode1
									.setPhysicalAttackOpportunity((int) (35));
							attackMode1.setMagicAttackOppotunity((int) (20));
							attackMode1.setLimitOppotunity(25);
							attackMode1.setFuZhuOppotunity((int) 20);

							monster.setAttackMode_normal(attackMode1);
							smallMonster.add(monster);
						}

						for (int i = 0; i < 5; i++) {
							MmochatMonsterFactory monster = new MmochatMonsterFactory();
							monster.setName("小仙童");
							monster.setLevel(level);
							monster.setFamily(MmochatFamilyType.人教);
							monster.setBmpType(MmochatRoleBmpType.人教男图);
							monster.setDao((int) (7 * MmochatBattleService
									.getStandardDao(level, 0)));
							monster.setCanRescure(false);
							monster.setAntiAll(30);
							monster.setLevelType(MmochatBossLevelType.自身等级);
							monster.setSuperPhysicalAttack(5);
							monster.setSuperSpritAttack(5);

							monster.setEquipLevel(20);
							monster.setTeamNum(8);
							monster.setKillRound(3);
							monster.setPhysical(0.3);
							monster.setMagic(0.2);
							monster.setBossMaxSkillTargetNum(4);
							monster.setSpeedPointAddType(1);

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							attackMode1
									.setPhysicalAttackOpportunity((int) (70));
							attackMode1.setMagicAttackOppotunity((int) (20));
							attackMode1.setFuZhuOppotunity((int) 10);
							monster.setAttackMode_normal(attackMode1);
							smallMonster.add(monster);
						}
						battle.setBoss(bigMonster);
						battle.setSmallBoss(smallMonster);
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
